package com.itheima.dao;

import com.itheima.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IRoleDao {

    /**
     * 根据用户id查询所有角色
     *
     * @param userId
     * @return
     */

    @Results(id = "roleMap", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "roleName", property = "roleName"),
            @Result(column = "roleDesc", property = "roleDesc"),
            @Result(property = "permissions", column = "id", javaType = java.util.List.class,
                    many = @Many(select = "com.itheima.dao.IPermissionDao.findPermissionByRoleID")),
    })
    @Select("select * from role where id in (select roleId from users_role where userId = #{userId} )")
    public List<Role> findRoleByUserId(String userId);

    /**
     * 查询所有角色
     *
     * @return
     */
    @Select("select * from role")
    List<Role> findAll();

    /**
     * 添加账户
     *
     * @param role
     */
    @Insert("insert into role(roleName,roleDesc) values(#{roleName},#{roleDesc})")
    void save(Role role);

    /**
     * 根据用户id查询用户没有的角色
     *
     * @param userId
     * @return
     */
    @Select("select * from role where id not in (select roleId from users_role where userId=#{userId} )")
    List<Role> findOtherRole(String userId);

    /**
     * 给角色添加权限
     * @param roleId,permissionId
     */
    @Insert("insert into role_permission (roleId,permissionId) values (#{roleId},#{permissionId})")
    void addPermissionToRole(@Param("roleId") String roleId,@Param("permissionId") String permissionId);

    @Select("select * from role where id = #{roleId}")
    Role findById(String roleId);
}
